package com.gj.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.ws.Response;

import com.gj.bean.ContactsBean;
import com.gj.bean.UserBean;
import com.gj.dao.ContactDao;
import com.gj.daoImp.ContactDaoImp;

/**
 * Servlet implementation class ContactServlet
 */
@WebServlet("/ContactServlet")
public class ContactServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ContactServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	public ContactsBean addContact(HttpServletRequest request) {
		HttpSession session = request.getSession();
		UserBean user = (UserBean) session.getAttribute("user");
		System.out.println("userid"+user.getUserId());
		System.out.println(request.getParameter("selectName"));
		ContactsBean book = new ContactsBean();
		book.setContactName(request.getParameter("username"));
		book.setContectNum(request.getParameter("telephone"));
		book.setMark(request.getParameter("mark"));
		book.setAddress(request.getParameter("address"));
		book.setCategoryId(Integer.parseInt(request.getParameter("selectName")));
		book.setUserId(user.getUserId());
		return book;
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		/*doGet(request, response);*/
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out=response.getWriter();
		
	    String type = request.getParameter("type");
	    out.println(type);
	    int result;
	ContactDao bookMng = new ContactDaoImp();
	if(type.equals("del"))

	{
		int ContactId = Integer.parseInt(request.getParameter("contactId"));
		result = bookMng.deleteContact(ContactId);
		if(result>0){
			 out.println("<script>alert('删除成功！！');</script>"); 
		     out.println("<script>window.location.href='jsp/mainPage.jsp'</script>");
			
		}
		if(result==-1 || result==0){
			out.println("<script>alert('删除失败！！');</script>");
			out.println("<script>window.location.herf='jsp/mainPage.jsp'");
		}
		if(result==-2){
			out.println("<script>alert('连接错误！！');</script>");
			out.println("<script>window.location.herf='jsp/mainPage.jsp'");
		}
	} 
   if(type.equals("add"))

	{
		ContactsBean book = addContact(request);
		result = bookMng.addContact(book);
		 out.print(result);
		if(result>0){
			 out.println("<script>alert('添加成功！！');</script>"); 
		     out.println("<script>window.location.href='jsp/mainPage.jsp'</script>");
			
		}
		if(result==-1 || result==0){
			out.println("<script>alert('添加失败！！');</script>");
			out.println("<script>window.location.herf='jsp/mainPage.jsp'");
		}
		if(result==-2){
			out.println("<script>alert('连接错误！！');</script>");
			out.println("<script>window.location.herf='jsp/mainPage.jsp'");
		}
	} 
	else {
	int contactId = Integer.parseInt(request.getParameter("contactId"));   
	 ContactsBean book = addContact(request);   
	 result = bookMng.UpdateContact(book,contactId); 
	 out.print(result);
	 if(result>0){
		 out.println("<script>alert('修改成功！！');</script>"); 
	     out.println("<script>window.location.href='jsp/mainPage.jsp'</script>");
		
	}
	if(result==-1 || result==0){
		out.println("<script>alert('修改失败！！');history.back();</script>");
		out.println("<script>window.location.herf='jsp/mainPage.jsp'");
	}
	if(result==-2){
		out.println("<script>alert('连接错误！！');</script>");
		out.println("<script>window.location.herf='jsp/mainPage.jsp'");
	}
	}
	}
}
